package com.cos.hospital.init;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.model.retrofit.HostWrapper;
import com.cos.hospital.model.retrofit.RequestServes;
import com.cos.hospital.tool.UDialog;
import com.cos.hospital.tool.ULog;
import com.cos.hospital.tool.UTxt;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @Description: <p>
 * @Author: Kosmos
 * @Date: 2018年08月02日 13:17
 * @Email: KosmoSakura@foxmail.com
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Context context;
    @Nullable
    @BindView(R.id.title_back)
    FrameLayout fLeft;
    @Nullable
    @BindView(R.id.title_back_pic)
    ImageView iLeft;
    @Nullable
    @BindView(R.id.title_txt)
    TextView tCenter;
    @Nullable
    @BindView(R.id.title_next_txt)
    TextView tRight;
    @Nullable
    @BindView(R.id.title_next)
    ImageView iRight;
    private CompositeDisposable compositeDisposable;
    private RequestServes rs;
    // 广播接收类
    private BroadcastReceiver cureEndBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(final Context context, final Intent intent) {
            if (intent == null || UTxt.isEmpty(intent.getAction())) {
                return;
            }
            ULog.commonD("收到广播：" + intent.getAction());
            if (Code.Broadcast.CUREEND.equals(intent.getAction())) {
                String content = intent.getStringExtra(Code.SP.CONTENT);
                cureEnd(content);
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        int layoutId = layout();
        if (layoutId != 0) {
            setContentView(layoutId);
            ButterKnife.bind(this);
        }
        init();
        logic();
        if (iLeft != null){
            iLeft.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    /**
     * @return 返回布局
     */
    protected abstract int layout();

    /**
     * 初始化基础信息
     */
    protected abstract void init();

    /**
     * 填充逻辑部分 include
     */
    protected abstract void logic();

    protected RequestServes getServes() {
        if (rs == null) {
            rs = HostWrapper.with().create(RequestServes.class);
        }
        return rs;
    }

    protected void addEvent(Disposable disposable) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(disposable);
    }

    protected void setTitleLeftIcon() {
        if (fLeft != null) {
            fLeft.setVisibility(View.VISIBLE);
        }
    }

    protected void setTitleLeftIcon(int res) {
        if (iLeft != null) {
            iLeft.setVisibility(View.VISIBLE);
            iLeft.setImageResource(res);
        }
    }

    protected void setTitleCenterTxt(String txt) {
        if (tCenter != null) {
            if (UTxt.isEmpty(txt)) {
                tCenter.setVisibility(View.GONE);
            } else {
                tCenter.setVisibility(View.VISIBLE);
                tCenter.setText(txt);
            }
        }
    }

    protected void setTitleRightTxt(String txt) {
        if (tRight != null) {
            if (UTxt.isEmpty(txt)) {
                tRight.setVisibility(View.GONE);
            } else {
                tRight.setVisibility(View.VISIBLE);
                tRight.setText(txt);
                if (iRight != null) {
                    iRight.setVisibility(View.GONE);
                }
            }
        }
    }

    protected void setTitleRightIcon(int res) {
        if (iRight != null) {
            iRight.setImageResource(res);
            if (tRight != null) {
                tRight.setVisibility(View.GONE);
            }
        }
    }

    /**
     * @param notice 显示页面错误提示
     */
    protected void showNotice(String notice) {
        UDialog.getInstance(context, false, false)
                .showNoticeWithOnebtn(notice);
    }

    protected void showNotice(String notice, UDialog.SureClick sureClick) {
        UDialog.getInstance(context, false, false)
                .showNoticeWithOnebtn(notice, sureClick);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (compositeDisposable != null) {
            compositeDisposable.clear();
        }
    }

    public void cureEnd(String content) {
        showNotice(content);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter cutoverFilter = new IntentFilter();
        cutoverFilter.addAction(Code.Broadcast.CUREEND);
        cutoverFilter.setPriority(100);
        registerReceiver(cureEndBroadcastReceiver, cutoverFilter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(cureEndBroadcastReceiver);
    }
}
